package uit.mmt02.activity;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;

import uit.mmt02.R;
import uit.mmt02.adapter.attach_adapter;
import uit.mmt02.doituong.attach;
import uit.mmt02.doituong.showToast;
import uit.mmt02.mailControl.GmailControl;
import uit.mmt02.mailControl.HotmailControl;
import uit.mmt02.mailControl.YahooControl;
import uit.mmt02.xuly.ConvertMessageToList;
import uit.mmt02.xuly.XuLyChuoi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class attach_download_Activity extends Activity
{
	final static String KEY_USERNAME = "username";
	final static String KEY_PASSWORD = "password";
	final static String KEY_LOAIEMAIL = "loai_email";
	final static String KEY_STR_ATTACH = "chuoi_attach";
	final static String KEY_STT = "_stt";
	ListView listview;
	List<attach>list;
    ArrayAdapter<attach>adapter;
    
    //
    int stt;
    String username;
    String password;
    Message []mess;
    ConvertMessageToList cv;
    XuLyChuoi xlchuoi ;
	String loai_email;
    GmailControl g;
    YahooControl y;
    HotmailControl h;
    String str_att;
    List<String>list_str_attach;
    String tag = "attach_download";
    showToast st;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attach_layout);
		//
		st = new showToast(this);
		username = getIntent().getStringExtra(KEY_USERNAME);
		password = getIntent().getStringExtra(KEY_PASSWORD);
		loai_email = getIntent().getStringExtra(KEY_LOAIEMAIL);
		str_att = getIntent().getStringExtra(KEY_STR_ATTACH);
		stt = getIntent().getIntExtra(KEY_STT, -1);
		//
		g = new GmailControl(username, password);
		y = new YahooControl(username, password);
		h = new HotmailControl(username, password);
		cv = new ConvertMessageToList();
		xlchuoi = new XuLyChuoi();
		//
		listview = (ListView)findViewById(R.id.lv_attach);
		list = new ArrayList<attach>();
		
		list_str_attach = xlchuoi.getList_str_attach(str_att);
		attach att = null;
		for(String str1 : list_str_attach)
		{
			if(!str1.equals("") && str1!= null )
				att = new attach(str1, true);
				Log.v(tag, "add att " + str1);
				list.add(att);
		}
		
		
		adapter = new attach_adapter(this, list);
		listview.setAdapter(adapter);
	}
	
	public void btn_onClick(View v)
	{
		Button btn = (Button)v;
		switch (btn.getId()) {
		case R.id.btn_attach_down:
			Log.v(tag, "dowbload");
			new save_async().execute("");
			break;

		default:
			break;
		}
	}
	
	public void save_att()
	{
		try 
		{
			if(loai_email.startsWith("gmail"))
				mess = g.getmail();
			else if(loai_email.startsWith("yahoo"))
			{
				mess = y.getmail();
			}	//
			else if(loai_email.startsWith("hotmail"))
			{
				//
				mess = h.getmail();
			}
			int i_down =  mess.length - stt;
//			Log.v(tag, mess[i_down].getSubject().toString());
			for(int i = 0 ; i < list.size(); i++)
			{
				if(list.get(i).isCheck())
				{
					Log.v(tag, "vao save _att");
					cv.Download_by_name(mess, i_down , i);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Err file download " + e.toString());
		}
	}
	
	private class save_async extends AsyncTask<String, Void, Void>
	{
		final ProgressDialog p = new ProgressDialog(attach_download_Activity.this);

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			String str_down = getResources().getString(R.string.downloading) + ": " + str_att;
			p.setMessage(str_down);
			p.show();
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			// TODO Auto-generated method stub
			Log.v(tag, "do in send .. async..");
			save_att();
			
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			
			p.dismiss();
//			open_forder_download();
//			d.showmess(getResources().getString(R.string.send_success));
			st.showmess(getResources().getString(R.string.down_success));
			super.onPostExecute(result);
		}
		
	}
	
	public void open_forder_download()
	{
		try
		{
			AlertDialog.Builder buider = new AlertDialog.Builder(this);
			buider.setMessage("Open forder Download ...");
			buider.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			buider.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					return;
				}
			});
			
			AlertDialog alert = buider.create();
			alert.show();
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "err open_download " +e.toString());
		}
	}
	
}
